﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities.PBKT;
using Entities.Admin;

namespace BLL.PBKT
{
	public class PlayerFootball
	{

		public static void AddCauThu(LichThiDau ltd, string tt)
		{
			string[] s = tt.Split('-');

			ChiTietTranDau cttd = new ChiTietTranDau();
			//cttd.MaLichThiDau = "LTD_00012";
			cttd.MaLichThiDau = ltd.MaLichThiDau;
			cttd.MaCT = s[0];
			int hinhThuc = 0;
			if (s[1].CompareTo("Ghi bàn") == 0)
				hinhThuc = 1;
			else if (s[1].CompareTo("Thẻ đỏ") == 0)
				hinhThuc = 2;
			else
				hinhThuc = 3;

			cttd.HinhThuc = hinhThuc;
			cttd.SoLuong = Int32.Parse(s[2]);
			if (ChiTietTranDau.Objects.InsertOrUpdate(cttd))
				if (hinhThuc == 1)
					PlayerFootball.Update(CauThu.Objects.GetById(cttd.MaCT));

		}
		public static bool Update(CauThu cauThu)
		{
			int soBT = 0;
			foreach (var item in ChiTietTranDau.Objects.Find(c => c.MaCT == cauThu.MaCT).ToList())
			{
				if (item.HinhThuc == 1)
					soBT += item.SoLuong;
			}

			return CauThu.Objects.Update(c => c.MaCT == cauThu.MaCT, c => new CauThu
			{
				SoBanThang = soBT
			});
		}
		public static List<CauThu> All(GiaiDau gd)
		{
			List<CauThu> dsCauThu = new List<CauThu>();
			List<DoiTuyen> dsDoiTuyen = DoiTuyen.Objects.Find(c => c.MaGD == gd.MaGiaiDau).ToList();
			foreach (var item in dsDoiTuyen)
				dsCauThu.AddRange(CauThu.Objects.Find(c => c.MaDT == item.MaDT).ToList());
			return dsCauThu;
		}
	}
}
